// Flutter imports:
import 'package:flutter/material.dart';

// Package imports:
import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:get/get.dart';

// Project imports:
import 'dropdown_button_logic.dart';

class DropdownButtonPage extends StatefulWidget {
  Function onSelect;
  List<String> func;

  List<IconData> funcIcon;

  DropdownButtonPage(
      {Key? key,
      required this.onSelect,
      required this.func,
      required this.funcIcon})
      : super(key: key);

  @override
  State<DropdownButtonPage> createState() =>
      _DropdownButtonPageState(onSelect, func, funcIcon);
}

class _DropdownButtonPageState extends State<DropdownButtonPage> {
  final logic = Get.put(DropdownButtonLogic());
  final state = Get.find<DropdownButtonLogic>().state;
  List<String> func;

  Function onSelect;
  List<IconData> funcIcon;

  _DropdownButtonPageState(this.onSelect, this.func, this.funcIcon);

  @override
  Widget build(BuildContext context) {
    return DropdownButtonHideUnderline(
      child: DropdownButton2(
        isDense: true,
        isExpanded: true,
        customButton: const Icon(
          Icons.list,
          size: 30,
          color: Colors.blueAccent,
        ),
        items: func.asMap().entries.map((e) {
          return DropdownMenuItem<String>(
            value: e.value,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Icon(funcIcon[e.key], color: Colors.black54, size: 20),
                const SizedBox(
                  width: 10,
                ),
                Expanded(
                  child: Text(
                    e.value,
                    style: const TextStyle(color: Colors.black54, fontSize: 12),
                  ),
                ),
              ],
            ),
          );
        }).toList(),
        onChanged: (value) {
          onSelect(func.indexOf(value ?? ""));
        },
        dropdownStyleData: DropdownStyleData(
          width: 150,
          padding: EdgeInsets.zero,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(4),
            color: Colors.white,
          ),
          offset: const Offset(0, 0),
        ),
      ),
    );
  }


  @override
  void dispose() {
    Get.delete<DropdownButtonLogic>();
    super.dispose();
  }
}
